<template>
  <!-- 消单确认 -->
  <div>
    <el-dialog
      v-el-drag-dialog
      title="消单确认"
      :visible.sync="visible"
      width="800px"
      @closed="reset"
    >
      <el-form ref="dataForm" class="form" label-width="100px" label-position="right">
        <el-form-item label="收 款 人" style="width: 50%;"><el-input v-model="temp.depReceiver" disabled /></el-form-item>
        <el-form-item label="收款方式" style="width: 50%;"><el-input v-model="calcType" disabled /></el-form-item>
        <el-form-item label="押瓶机构" style="width: 50%;"><el-input v-model="temp.ckOrgName" disabled /></el-form-item>
        <el-form-item label="退瓶机构" style="width: 50%;"><el-input v-model="temp.returnOrgId" disabled /></el-form-item>
        <el-form-item label="开始时间" style="width: 50%;"><el-input v-model="startDt" disabled /></el-form-item>
        <el-form-item label="结束时间" style="width: 50%;"><el-input v-model="endDt" disabled /></el-form-item>
        <el-form-item label="总 押 金" style="width: 50%;"><el-input v-model="temp.totalDeposit" disabled /></el-form-item>
        <el-form-item label="总 租 金" style="width: 50%;"><el-input v-model="temp.totalRental" disabled /></el-form-item>
        <el-form-item label="应退金额" style="width: 50%;"><el-input v-model="money" disabled /></el-form-item>
        <el-form-item label="实退金额" style="width: 50%;"><el-input v-model="temp.accReturnDeposit" disabled /></el-form-item>
        <el-form-item label="消单时间" style="width: 50%;">
          <el-date-picker
            v-model="atTime"
            style="width: 100%;"
            type="datetime"
            format="yyyy-MM-dd HH:mm"
            :clearable="false"
            placeholder="选择日期"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="pass(3)">通过</el-button>
        <el-button type="primary" @click="pass(2)">不通过</el-button>
        <el-button @click="reset">关闭</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import { formatColumnMap, parseTime } from '@/utils'
  import { queryMap, queryCashPledgeReturnConfirm } from '@/api'

  export default {
    name: 'orderconfirmation',
    data() {
      return {
        visible: false,
        atTime: new Date(),
        calcTypeList: [],
        temp: {}
      }
    },
    computed: {
      // 收款方式
      calcType: function() {
        return formatColumnMap(this.calcTypeList, 'value', this.temp.calcType, 'desc')
      },
      // 开始时间
      startDt: function() {
        return this.temp.startDt && parseTime(this.temp.startDt, '{y}-{m}-{d} {h}:{i}:{s}')
      },
      // 结束时间
      endDt: function() {
        return this.temp.endDt && parseTime(this.temp.endDt, '{y}-{m}-{d} {h}:{i}:{s}')
      },
      // 应退金额  总押金减去总租金
      money: function() {
        return this.temp.totalDeposit - this.temp.totalRental
      }
    },
    async created() {
      await this.queryOptionsMap()
    },
    methods: {
      open(temp) {
        console.log('temp', temp)
        this.temp = temp
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
        })
      },
      // 3通过 2不通过
      async pass(depStatus) {
        const dt = this.atTime
        const dt1 = new Date()
        if (Date.parse(this.temp.auditDt) > Date.parse(dt)) {
          this.$message({
            type: 'warning',
            message: '消单时间不能小于审核时间'
          })
          return
        }
        if (Date.parse(dt) > Date.parse(dt1)) {
          this.$message({
            type: 'warning',
            message: '消单时间不能大于当前时间'
          })
          return
        }
        const { status } = await queryCashPledgeReturnConfirm({
          depStatus: depStatus,
          depositCode: this.temp.depositCode,
          operateDt: parseTime(String(this.atTime), '{y}-{m}-{d} {h}:{i}')
        })
        if (status === 200) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.$emit('upDataTable')
          this.visible = false
        }
      },
      // 关闭
      reset() {
        this.visible = false
        this.atTime = new Date()
      },
      // 收款方式
      async queryOptionsMap() {
        try {
          const { data } = await queryMap({ enumName: 'CalcType' })
          this.calcTypeList = data
        } catch (error) {
          console.log(error)
        }
      }
    }
  }
</script>

<style lang='scss' scoped>
.form {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}
</style>
